<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #instance mode - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23instance+mode</link>
      <pubDate>Sun, 08 Aug 2021 17:18:42 +0000</pubDate>
         <description>Tagged with #instance mode - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23instance+mode/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Is object creation delayed when managed into a P5js instance?</title>
      <link>https://forum.processing.org/two/discussion/27932/is-object-creation-delayed-when-managed-into-a-p5js-instance</link>
      <pubDate>Mon, 07 May 2018 08:48:12 +0000</pubDate>
      <dc:creator>nemecsek69</dc:creator>
      <guid isPermaLink="false">27932@/two/discussions</guid>
      <description><![CDATA[<p>As my knowledge of JS is extremely limited, I cannot properly describe the issue: it is more a curiosity than a real problem (it took me some time to pinpoint it).</p>

<p>Here I am creating 2 objects, setting the <code>type</code> variable ONLY AFTER calling the object creation.
It looks like the object is not created immediately but only in a second time, after the variable has been set, even if the lines order would hint otherwise.</p>

<p>Is this JS or P5.js related? Is this due to <strong>hoisting</strong>?</p>

<p>I would like, for my own knowledge, to be directed to the proper docs in order to understand what is happening here.
Thanks</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
    &lt;script type="text/javascript" src="static/js/p5.min.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;script&gt;
        let sketch = function (p) {
            p.setup = function () {
                alert("Creating " + p.type);
            };
        };

        let objA = new p5(sketch);
        objA.type = "A";      // &lt;--- AFTER creating the object?!?

        let objB = new p5(sketch);
        objB.type = "B";
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
]]></description>
   </item>
   <item>
      <title>Converting p5 sketch automatically to instance mode</title>
      <link>https://forum.processing.org/two/discussion/27881/converting-p5-sketch-automatically-to-instance-mode</link>
      <pubDate>Mon, 30 Apr 2018 08:55:47 +0000</pubDate>
      <dc:creator>animatebig</dc:creator>
      <guid isPermaLink="false">27881@/two/discussions</guid>
      <description><![CDATA[<p>Is there any tool available which lets us convert plain p5 sketch into instance mode automatically? Even if its not there. Is it practically possible to convert it using regular expressions?</p>
]]></description>
   </item>
   <item>
      <title>convert P5js global mode to instance mode with script</title>
      <link>https://forum.processing.org/two/discussion/25828/convert-p5js-global-mode-to-instance-mode-with-script</link>
      <pubDate>Fri, 05 Jan 2018 01:23:15 +0000</pubDate>
      <dc:creator>cimenx</dc:creator>
      <guid isPermaLink="false">25828@/two/discussions</guid>
      <description><![CDATA[<p>Hello, I'm still new to this community.</p>

<p>I have just build <a rel="nofollow" href="https://github.com/DrSensor/p5-global2instance">p5-global2instance</a>. 
It's node.js script for converting P5js global mode to instance mode.</p>

<p>Looking for feedback! :smile:</p>

<blockquote class="Quote">
  <p>That's script can be the basis for building <a rel="nofollow" href="https://webpack.js.org/loaders/">webpack-loader</a> for p5 sketch.</p>
</blockquote>
]]></description>
   </item>
   <item>
      <title>Variables in p5.js functions</title>
      <link>https://forum.processing.org/two/discussion/27778/variables-in-p5-js-functions</link>
      <pubDate>Mon, 16 Apr 2018 09:29:27 +0000</pubDate>
      <dc:creator>moo</dc:creator>
      <guid isPermaLink="false">27778@/two/discussions</guid>
      <description><![CDATA[<p>Ok, So I am struggling with finding any information on variables.</p>

<p>the code that works looks like this:
Basically its moving the triangle to the left.</p>

<pre><code>  var a, b, c
  var counter

  p.setup = () =&gt; {
    p.createCanvas(1600, 200);
    b = 800; a = 800; c = 800
    counter = 0
  }

  function rotation(degrees) {
    p.rotate(p.radians(degrees))
  }

  function triangleLeft() {
      a &lt; 0 ? a = 800 :a = a-2;
      console.log(a)
      p.fill(209, 238, 255);
      p.translate(a, 100);
        p.triangle(0, -50, -50, 0, 0, 50)
  }

  p.draw = () =&gt; {
    counterTimer()
    p.background(89, 113, 255);
    p.noStroke();
      triangleLeft()
}
</code></pre>

<p>but I want to use the tringleLeft() function a bunch of times, if I try and do:</p>

<pre><code>  function triangleLeft(num) {
      num &lt; 0 ? num = 800 :num = num-2;
      console.log(num)
      p.fill(209, 238, 255);
      p.translate(num, 100);
        p.triangle(0, -50, -50, 0, 0, 50)
  }

  p.draw = () =&gt; {
    counterTimer()
    p.background(89, 113, 255);
    p.noStroke();
      triangleLeft(a)
}
</code></pre>

<p>it doesn't increase the variable, it just stays at 800.. any ideas?</p>
]]></description>
   </item>
   <item>
      <title>Two Canvas, problem with positions.</title>
      <link>https://forum.processing.org/two/discussion/26864/two-canvas-problem-with-positions</link>
      <pubDate>Thu, 15 Mar 2018 17:42:32 +0000</pubDate>
      <dc:creator>DDv2FF</dc:creator>
      <guid isPermaLink="false">26864@/two/discussions</guid>
      <description><![CDATA[<p>Hi. I wanted to create more canvas in my page, and I found this.<br />
<a rel="nofollow" href="https://github.com/processing/p5.js/wiki/Instantiation-Cases">https://github.com/processing/p5.js/wiki/Instantiation-Cases</a><br />
And I have two canvas now, but when i pass an argument for the node, it doesn't work. I want to give a canvas to each paragraph. Also, I cant use createElements , position, or parent, etc, into the instances . What is missing ? Or what im doing wrong ?</p>

<pre><code>var mycanvas = function(canvas1){
    canvas1.setup = function(){
        canvas1.createCanvas(500, 500);
        canvas1.background(0);
    };
};

var my2canvas = function(canvas2){
    canvas2.setup = function(){
        canvas2.createCanvas(500, 500);
        canvas2.background(255, 0, 0);
    };
};
new p5(mycanvas, "#fcanvas");
new p5(my2canvas, "#scanvas");
</code></pre>

<p>And the Html</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;

    &lt;title&gt;twoCanvas&lt;/title&gt;

    &lt;script src="libraries/p5.js"&gt;&lt;/script&gt;
    &lt;script src="libraries/p5.dom.js"&gt;&lt;/script&gt;
    &lt;script src="libraries/p5.sound.js"&gt;&lt;/script&gt;
    &lt;script src="sketch.js"&gt;&lt;/script&gt;

    &lt;style&gt;

    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/p id = "fcanvas"&gt;First Canvas&lt;/p&gt;
&lt;/p id = "scanvas"&gt;Second Canvas&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
]]></description>
   </item>
   <item>
      <title>Chrome extensions with gif in canvas</title>
      <link>https://forum.processing.org/two/discussion/25534/chrome-extensions-with-gif-in-canvas</link>
      <pubDate>Tue, 12 Dec 2017 13:49:01 +0000</pubDate>
      <dc:creator>Layzfat</dc:creator>
      <guid isPermaLink="false">25534@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I'm follow Shiffman incredible lessons on using P5js with chrome extensions.</p>

<p>In the example of the Canvas sketch with chrome extensions I followed, I wanted to do some more. Add gifs in the canvas overlaying the top layer of the browser viewing.</p>

<p>My attempt was unsuccessful.. The gif doesn't play..</p>

<p>So what did I do. 
I used <a rel="nofollow" href="https://p5js.org/examples/image-load-and-display-image.html">this example</a> to import gif in to p5js 
put it in my sketch.js as this:</p>

<pre><code>console.log('sketch blah');

var s = function(sketch) {
var img; //declare image

sketch.setup = function() {
  document.body.style['userSelect'] = 'none';
  let h = document.body.clientHeight;
  let c = sketch.createCanvas(sketch.windowWidth, h);
  img = sketch.loadImage("https://" + "media.giphy.com/media/xUOxfnlpG1tsYGFI40/giphy.gif");
  c.position(0,0);
  c.style('pointer-events', 'none');
  sketch.clear();
}

sketch.draw = function() {
  console.log('sketch Looping');
  sketch.image(img, 0, 0);
  //sketch.background(0);
  }
};

var myp5 = new p5(s);
</code></pre>

<p>My manifest.json looks like this.</p>

<pre><code>  {
    "manifest_version": 2,
    "name": "Agent Extension",
    "version": "0.0002",
    "web_accessible_resources": [
      "gif/*.gif"
    ],
    "content_scripts": [
      {
        "matches": [
            "&lt;all_urls&gt;"
        ],
        "js": ["p5.js","p5.dom.js","p5.gif.js","p5.gif.min.js", "sketch.js"]

      }
    ],
    "background": {
      "scripts": ["background.js"]
    },
    "browser_action": {
      "default_icon": "img.png"
    }
  }
</code></pre>

<p>all the files are in the wright place.</p>

<p>I get this error in the browsers console</p>

<pre><code>sketch.js:18 Uncaught ReferenceError: image is not defined
    at sketch.draw (sketch.js:18)
    at p5.redraw (p5.js:50266)
    at p5.&lt;anonymous&gt; (p5.js:44917)
    at p5.&lt;anonymous&gt; (p5.js:44812)
sketch.draw @ sketch.js:18
p5.redraw @ p5.js:50266
(anonymous) @ p5.js:44917
(anonymous) @ p5.js:44812
</code></pre>

<p>I did define it, my guess it is not defined for the browsers standards</p>

<p>I want to have a few 'objects' on the canvas and other types of code taking over the content as end goal.</p>
]]></description>
   </item>
   <item>
      <title>Animating with large datasets</title>
      <link>https://forum.processing.org/two/discussion/25225/animating-with-large-datasets</link>
      <pubDate>Mon, 27 Nov 2017 10:51:19 +0000</pubDate>
      <dc:creator>CPG</dc:creator>
      <guid isPermaLink="false">25225@/two/discussions</guid>
      <description><![CDATA[<p>Hi Everyone!</p>

<p>I have been working with P5 for a while now, but in my current project I'm working with large data sets for the first time.
The FPS I'm getting when animating over 2000+ data points is not good, is there a way to improve upon this?</p>

<p>Thanks!</p>
]]></description>
   </item>
   <item>
      <title>Instance mode over multiple files</title>
      <link>https://forum.processing.org/two/discussion/23580/instance-mode-over-multiple-files</link>
      <pubDate>Tue, 25 Jul 2017 17:44:00 +0000</pubDate>
      <dc:creator>ruairidh</dc:creator>
      <guid isPermaLink="false">23580@/two/discussions</guid>
      <description><![CDATA[<p>Hi,</p>

<p>I'm writing some sketches in p5 using instance mode. Some of them are getting pretty big, so I was wondering if there was any way to move some of the functionality into multiple files. My initial attempt was to move all the mouse event functions into another file, then import it into the main sketch file and call a function to register them:</p>

<p>test.js:</p>

<pre><code>import registerFunctions from './mouseHandler.js';
let sketch = function(p5) {

  var a = 5;

  p5.setup = function() {
    ...
    registerFunctions(p5);
  };

  p5.draw = function() {...};
};

let p5Sketch = new p5(sketch , 'canvas-holder');
</code></pre>

<p>mouseHandler.js:</p>

<pre><code>function _mousePressed() {
  console.log(a); // Obviously wont work
}

export default function registerInputFunctions(p5) {

    p5.mousePressed = _mousePressed;
}
</code></pre>

<p>My obvious issue is that I can't access the inner variables from test.js, so this method wont be very useful. So is there any way I can split the sketch into multiple files and somehow access the variables in the sketch from each of the other files?</p>

<p>Thanks in advance  :)&gt;-</p>
]]></description>
   </item>
   <item>
      <title>instance mode</title>
      <link>https://forum.processing.org/two/discussion/23438/instance-mode</link>
      <pubDate>Thu, 13 Jul 2017 22:07:51 +0000</pubDate>
      <dc:creator>jmm</dc:creator>
      <guid isPermaLink="false">23438@/two/discussions</guid>
      <description><![CDATA[<p>I try mode instance.
I'm missing something but what ?
Here the code   which does not work : 
<a href="https://jsfiddle.net/teL27db8/" target="_blank" rel="nofollow">https://jsfiddle.net/teL27db8/</a></p>

<p>If I add  function setup() {}, 
the  figures appear :
<a href="https://jsfiddle.net/e12tbs22/" target="_blank" rel="nofollow">https://jsfiddle.net/e12tbs22/</a></p>

<p>Thank you for any explanation</p>
]]></description>
   </item>
   <item>
      <title>Instance Mode Error: Failed to load resource: net::ERR_FILE_NOT_FOUND Auto:1</title>
      <link>https://forum.processing.org/two/discussion/22980/instance-mode-error-failed-to-load-resource-net-err-file-not-found-auto-1</link>
      <pubDate>Wed, 07 Jun 2017 22:40:57 +0000</pubDate>
      <dc:creator>destinyschilde</dc:creator>
      <guid isPermaLink="false">22980@/two/discussions</guid>
      <description><![CDATA[<p>This is embarrassing...So here's the problem:
I created this very nice nav button for my site; its a p5 sketch. I wanted to add another canvas to that same page and realized that i couldn't. I realized that i needed to use instance mode, so read all about it and after a lot of tinkering i got my button to display. The problem is that i cant click on it and it displays this error:</p>

<p>Failed to load resource: net::ERR_FILE_NOT_FOUND   Auto:1</p>

<p>i know its a little bit very messy, but its really simple and im not sure where the issue is. I suspect it has something to do with how im referencing DOM elements, but i cant find any examples of this being done in instance mode.</p>

<p>all this really is is a little div that when clicked, enlarges to fit the screen.</p>

<p><code>
var sketch = function (p) {
var link1;
var link2;
var link3;
var canva;
var cnv;
open=false;
p.setup=function() {
  link1 = p.createA('index.html', 'Who?');
  link1.position(0,0);
  link2 = p.createA('<a href="http://p5js.org'" target="_blank" rel="nofollow">http://p5js.org'</a>, 'Projects');
  link2.position(0,100);
  link3 = p.createA('<a href="http://p5js.org'" target="_blank" rel="nofollow">http://p5js.org'</a>, 'Games');
  link3.position(0,200);
  link4 = p.createA('<a href="http://p5js.org'" target="_blank" rel="nofollow">http://p5js.org'</a>, 'Experiments');
  link4.position(0,300);
  canva = p.select('#navo');
  bod = p.select('body');
  canva.style('width',"0%");
  cnv = p.createCanvas(110,110); /<em>this is just to nest it in a div</em>/
  cnv.parent("navo");
  link1.parent("inner");
  link2.parent("inner");
  link3.parent("inner");
  link4.parent("inner");
p.colorMode(p.HSB);
x=110;
speed=0;
segmentx=29;
segmenty=13;
open=false;
};
p.draw=function() {
  p.clear()
    x=x+speed;
  if (x&gt;p.windowWidth+1000){
  speed=0;
  }
  if ((p.mouseX&gt;0)&amp;&amp;(p.mouseX&lt;70)&amp;&amp;(p.mouseY&gt;0)&amp;&amp;(p.mouseY&lt;70)){
  p.cursor(p.HAND);
  }else {
  p.cursor(p.AUTO);
  }
  if (x&lt;p.windowWidth/2){
    link1.hide();
  } else {
    link1.show();
  }
  if (x&lt;p.windowWidth){
    link2.hide();
  } else {
    link2.show();
  }
  if (x&lt;p.windowWidth+200){
    link3.hide();
  } else {
    link3.show();
  }
  if (x&lt;p.windowWidth+350){
    link4.hide();
  } else {
    link4.show();
  }
  if (x&gt;110){
  segmentx=-10;
  segmenty=-10;
  open==true;
  }
  if (x&lt;110){
    x=110;
  speed=0;
  segmentx=29;
  segmenty=13;
  open=false;
  desizeit();
  }
p.noStroke();
p.fill(p.frameCount % 360, 255, 255);
p.triangle(0, 0, 0,x, x, 0);
p.textAlign(p.CENTER);
p.fill(0);
p.fill(255);
p.rect(segmentx,segmenty, 8,40);
p.rect(13,29,40,8);
};
p.mouseReleased=function() {
  if ((p.mouseX&gt;0)&amp;&amp;(p.mouseX&lt;70)&amp;&amp;(p.mouseY&gt;0)&amp;&amp;(p.mouseY&lt;70)&amp;&amp;(p.open==false)){
  speed=70;
  resizeit();
  open=true;
  }
  if ((p.mouseX&gt;0)&amp;&amp;(p.mouseX&lt;70)&amp;&amp;(p.mouseY&gt;0)&amp;&amp;(p.mouseY&lt;70)&amp;&amp;(x&gt;p.windowWidth+1000)){
  speed=-100;
  }
};
p.resizeit=function() {
    p.resizeCanvas(p.windowWidth, p.windowHeight);
    canva.style('width',"100%");
    bod.style('overflow-y',"hidden");
};
p.desizeit=function() {
    p.resizeCanvas(110, 110);
    canva.style('width',"0%");
    bod.style('overflow-y',"visible");
};
};
new p5(sketch);</code></p>

<p></p>
]]></description>
   </item>
   <item>
      <title>How to trigger programmaticaly the draw method of a sketch in 'instance' mode?</title>
      <link>https://forum.processing.org/two/discussion/22310/how-to-trigger-programmaticaly-the-draw-method-of-a-sketch-in-instance-mode</link>
      <pubDate>Mon, 01 May 2017 04:24:28 +0000</pubDate>
      <dc:creator>radumir</dc:creator>
      <guid isPermaLink="false">22310@/two/discussions</guid>
      <description><![CDATA[<p>Hi!</p>

<p>I just want to use noLoop() in setup and therefore call only once the draw method but I want to update (to draw again) the sketch in some circumstances.</p>

<p>What's the best advice to achieve this?</p>

<p>I saw that in 'instance' mode it is needed a function f(sketch) that inside defines sketch.setup and sketch.draw and returns nothing.</p>

<p>I intend to define inside a function, let's say drawMethod and make the sketch.draw = drawMethod and finally return an object { draw: drawMethod } in order to expose the method to the outside world and be able to be called later.</p>

<p>I'm not sure if this is the recommended way or not.</p>

<p>Kind regards,</p>

<p>Radu Mirescu</p>
]]></description>
   </item>
   <item>
      <title>Switching logic is not working</title>
      <link>https://forum.processing.org/two/discussion/21638/switching-logic-is-not-working</link>
      <pubDate>Mon, 27 Mar 2017 14:22:08 +0000</pubDate>
      <dc:creator>blyk</dc:creator>
      <guid isPermaLink="false">21638@/two/discussions</guid>
      <description><![CDATA[<p>Okay, It may look like this question should be under hardware category but please bear with me, it is not a hardware realted question.</p>

<p>I have been trying to create two canvas using instantiation mode in p5js where when you take an input from sensor, it switches the canvas back-forth and activates one canvas at a time and set the opacity to 50% of other canvas.</p>

<p>I have been using Neurosky mindwave to take eyeblink input from Neurosky EEG sensor and when user blinks the it should activate one canvas and deactivates another one. The sketch works fine when I use <strong>mouseIsPressed</strong> and switch back-forth but it doesn't work with the sensor. Note: - It switch the canvas but only once and doesn't switch back to previous state when using Neurosky sensor.</p>

<p>I am using this node module for Neurosky -  <a rel="nofollow" href="https://github.com/dluxemburg/node-neurosky">MODULE</a></p>

<p>**Code - **</p>

<pre><code>    sketch.js // contains two sketches

    var stateTwo, stateOne = true;
    var k = 0;
    // sketch one -----------------------------------

    var first = new p5(firstSketch, "canvasOne");

    function firstSketch(p) {

        p.setup = function() {
            p.createCanvas(400, 250);
        }
        p.draw = function() {
            p.background(255, 10, 100);
            p.fill(255);
            p.ellipse(p.width / 2, p.height / 2, 50, 50);
            if (blink &gt; 30) {
                stateOne = false;
                stateTwo = true;
                console.log(k + " &lt;-- one");
               // k = 0;
                blink = 0;
            }
            if (stateOne) {
                $('#canvasOne').css('opacity', '1');
                $('#canvasTwo').css('opacity', '0.5');
                // console.log("canvas One");
                p.fill(255, 0, 0);
                p.ellipse(p.random(p.width), p.random(p.height), 50, 50);
            }
        }
    }

    // sketch two -----------------------------------

    var second = new p5(secondSketch, "canvasTwo");

    function secondSketch(p) {

        p.setup = function() {
            p.createCanvas(400, 250);
        }
        p.draw = function() {
            p.background(60, 250, 100);
            p.fill(0);
            p.ellipse(p.width / 2, p.height / 2, 50, 50);

            if (blink &gt; 30) {
                stateOne = true;
                stateTwo = false;
                console.log(k + " &lt;-- two");
              //  k = 0;
                blink = 0;
            }

            if (stateTwo) {
                $('#canvasOne').css('opacity', '0.5');
                $('#canvasTwo').css('opacity', '1');
                // console.log("canvas Two");
                p.fill(0, 0, 255);
                p.ellipse(p.random(p.width), p.random(p.height), 50, 50);
            }
        }
    }
</code></pre>

<p><strong>HTML</strong></p>

<pre><code>        &lt;!DOCTYPE html&gt;
        &lt;html lang="en"&gt;

        &lt;head&gt;
            &lt;meta charset="UTF-8"&gt;
            &lt;title&gt;CANVAS&lt;/title&gt;
            &lt;link rel="stylesheet" href="css/custom.css"&gt;
        &lt;/head&gt;

        &lt;body&gt;
            &lt;div&gt;
                &lt;div id="canvasOne"&gt;&lt;/div&gt;
                &lt;div id="canvasTwo"&gt;&lt;/div&gt;
            &lt;/div&gt;
            &lt;script type="text/javascript" src="js/jquery-2.1.1.min.js"&gt;&lt;/script&gt;
            &lt;script type="text/javascript" src="js/p5.js"&gt;&lt;/script&gt;
            &lt;script type="text/javascript" src="js/p5.dom.js"&gt;&lt;/script&gt; 
            &lt;script type="text/javascript" src="js/connect.js"&gt;&lt;/script&gt;
            &lt;script type="text/javascript" src="js/sketch.js"&gt;&lt;/script&gt;
        &lt;/body&gt;

        &lt;/html&gt;
</code></pre>

<p><strong>CONNECT.js</strong></p>

<pre><code>             var attention = 0;
             var meditation = 0;
             var blink;
             var poorSignalLevel = 0;

             if ("WebSocket" in window) {
                 console.log("WebSocket is supported by your Browser. Proceed.");
                 // $('#connect-controls').show();
             }

             var ws = new WebSocket("ws://127.0.0.1:8080");
             ws.onopen = function() {
                 console.log('opened connection');
                 ws.send("Hello from websocket client!");
             };

             // whenever websocket server transmit a message, do this stuff
             ws.onmessage = function(evt) {
                 // parse the data (sent as string) into a standard JSON object (much easier to use)
                 var data = JSON.parse(evt.data);
                 // handle "eSense" data
                 if (data.eSense) {
                     $('#brain').css({
                         opacity: 1
                     });
                     attention = data.eSense.attention;
                     meditation = data.eSense.meditation;
                     // brainProgress('#focusProgress', attention);
                     // brainProgress('#medProgress', meditation);
                     $("#focus").text(attention);
                     $("#meditation").text(meditation);
                 }

                 // handle "blinkStrength" data
                 if (data.blinkStrength) {
                     blink = data.blinkStrength;
                     var blinkcol = "white";
                     var eyeVal = map_range(blink, 0, 255, 0, 100);
                     $('#eyeBlinkStrength').text(parseInt(eyeVal));
                     if (blink &gt; 25) {
                         //blinkcol = "rgba(102,211,43,1.0)";
                         k++;   
                         console.log(blink + " " +k);
                     } else blinkcol = "white";
                     $('#eyeBlink').css({
                         width: eyeVal,
                         height: eyeVal,
                         background: blinkcol
                     });
                 } else {
                     blink = 0;
                 }

                 // handle "poorSignal" data
                 if (data.poorSignalLevel != null) {
                     poorSignalLevel = parseInt(data.poorSignalLevel);
                 }
             };

             // when websocket closes connection, do this stuff
             ws.onclose = function() {
                 // websocket is closed.
                 console.log("Connection is closed...");
             };


             function map_range(value, low1, high1, low2, high2) {
                 return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
             }
</code></pre>
]]></description>
   </item>
   <item>
      <title>DOM interfering with Sketch Container</title>
      <link>https://forum.processing.org/two/discussion/21532/dom-interfering-with-sketch-container</link>
      <pubDate>Wed, 22 Mar 2017 06:11:46 +0000</pubDate>
      <dc:creator>yovendofruta</dc:creator>
      <guid isPermaLink="false">21532@/two/discussions</guid>
      <description><![CDATA[<p>My goal is to have these sketches centered in the horizontal direction, and one appear first on the page, then the second. Here is my script file. These two sketches in instance mode are identical save the image loaded into each one.</p>

<pre><code>var PH2 = function ( p ) {
    var mic;
    var img;
    var myCanvas;

    p.centerCanvas = function() {
        var x = (p.windowWidth - p.width) / 2;
        myCanvas.position(x, 0);
        }          

    p.preload = function() {
        img = p.loadImage('Sketches/PH2/1.jpg');
        }

    p.setup = function() {
        myCanvas = p.createCanvas(1000, 600);
        mic = new p5.AudioIn()
        mic.start();
        p.centerCanvas();
        }

    p.windowResized = function() {
        p.centerCanvas();
        }            

    p.draw = function() {
        if (p.mouseIsPressed) {
            micLevel = mic.getLevel();
            p.background('#fdfaee');
            p.textFont("Helvetica");
            p.text("do", 500*micLevel+200, 600*micLevel+300);
            p.fill('#f99991');
            }

        else {
            micLevel = mic.getLevel();
            p.background(255);    
            p.image(img, 200*micLevel*50+200, -50*micLevel*50, p.width/1.9, p.height);
            }
        }
    }
myp5 = new p5(PH2, 'PH2Contain');

var PH3 = function ( p ) {
    var mic;
    var img;
    var myCanvas;

    p.centerCanvas = function() {
        var x = (p.windowWidth - p.width) / 2;
        myCanvas.position(x, 0);
        }          

    p.preload = function() {
        img = p.loadImage('Sketches/PH3/2.jpg');
        }

    p.setup = function() {
        myCanvas = p.createCanvas(1000, 600);
        mic = new p5.AudioIn()
        mic.start();
        p.centerCanvas();
        }

    p.windowResized = function() {
        p.centerCanvas();
        }            

    p.draw = function() {
        if (p.mouseIsPressed) {
            micLevel = mic.getLevel();
            p.background('#fdfaee');
            p.textFont("Helvetica");
            p.text("you", 500*micLevel+700, 600*micLevel+300);
            p.fill('#bcf5ee');
            }

        else {
            micLevel = mic.getLevel();
            p.background(255);    
            p.image(img, 300*micLevel*50+200, -100*micLevel*50, p.width/1.9, p.height);
            }
        }
    }
myp5 = new p5(PH3, 'PH3Contain');
</code></pre>

<p>Here is my HTML</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;link rel="stylesheet" type="text/css" href="PaintStyle.css"&gt;.
        &lt;script src="Resources/p5.js"&gt;&lt;/script&gt;
        &lt;script language="javascript" type="text/javascript" src="Resources/p5.dom.js"&gt;&lt;/script&gt;
        &lt;script language="javascript" type="text/javascript" src="Resources/p5.sound.js"&gt;&lt;/script&gt;
        &lt;script language="javascript" type="text/javascript" src="Sketches/PH2/PH2.js"&gt;&lt;/script&gt;
        &lt;title&gt;painthead&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;

&lt;div id="PH2Contain"&gt;&lt;/div&gt;
&lt;p&gt;hello hello&lt;/p&gt;
&lt;div id="PH3Contain"&gt;&lt;/div&gt;  

    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>If I do not include the centering operations the sketches appear correct but are aligned left (and I want them centered). Whenever I include the function to center the sketch I only see the second sketch centered with "hello hello" to the left of it. With this method I am assuming in myCanvas.position(x,y) the y value is relative to the beginning of the div, but this must not be the case?</p>
]]></description>
   </item>
   <item>
      <title>Instance Mode Trouble Shooting for 2+ Sketches on 1 Webpage</title>
      <link>https://forum.processing.org/two/discussion/20744/instance-mode-trouble-shooting-for-2-sketches-on-1-webpage</link>
      <pubDate>Sat, 11 Feb 2017 07:04:44 +0000</pubDate>
      <dc:creator>yovendofruta</dc:creator>
      <guid isPermaLink="false">20744@/two/discussions</guid>
      <description><![CDATA[<p>I am attempting to implement more than one sketch on an html page using instance mode, but keep getting errors thrown at me in the Processing IDE. I am using p5js.</p>

<p>Here is the sketch I am attempting to convert to instance mode.</p>

<pre><code>var img;
  function preload() {
      img = loadImage("<a href="https://dl.dropboxusercontent.com/u/55913541/1.jpg" target="_blank" rel="nofollow">https://dl.dropboxusercontent.com/u/55913541/1.jpg</a>");
 }
function setup() {
  var myCanvas = createCanvas(1000, 600);
  myCanvas.parent('1stContain');
  mic = new p5.AudioIn()
  mic.start();
}
function draw() {
  background(255);
  if(mouseIsPressed) {
    micLevel = mic.getLevel();
    var c = color('#f99991');
    var v = color('#fdfaee');
    background(v);
    textFont("Helvetica");
    text("do", 500*micLevel+200, 600*micLevel+300);
    fill(c);
  }
  else {
  micLevel = mic.getLevel();
  image(img, 200*micLevel*50, -50*micLevel*50, img.width/2, img.height/2);
  }
}
`

Here is my attempt 

`
var sketch = function ( p ) {
var img;
  function preload() {
    img = loadImage("<a href="https://dl.dropboxusercontent.com/u/55913541/1.jpg" target="_blank" rel="nofollow">https://dl.dropboxusercontent.com/u/55913541/1.jpg</a>");
  }
  p.setup = function() {
    var myCanvas = p.createCanvas(1000, 600);
    myCanvas.parent('1stContain');
    mic = p.new p5.AudioIn()
    mic.start();
}
  p.draw = function() {
    p.background(255);
  if(mouseIsPressed) {
    micLevel = mic.getLevel();
    p.var c = color('#f99991');
    p.var v = color('#fdfaee');
    p.background(v);
    p.textFont("Helvetica");
    p.text("do", 500*micLevel+200, 600*micLevel+300);
    p.fill(c);
  }
  else {
    p.micLevel = mic.getLevel();
    p.image(img, 200*micLevel*50, -50*micLevel*50, img.width/2, img.height/2);
  }
}
}
new p5(sketch);
</code></pre>

<p>I keep getting the error "SyntaxError Expected; but found myCanvas"
In the long run I am trying to get 10+ sketches running together on one html page. When I try to implement two (using non instance mode) only the second one shows on the page. I can also include my HTML code if that will help.</p>
]]></description>
   </item>
   <item>
      <title>Function, This and Array</title>
      <link>https://forum.processing.org/two/discussion/20958/function-this-and-array</link>
      <pubDate>Thu, 23 Feb 2017 00:47:11 +0000</pubDate>
      <dc:creator>BoJack</dc:creator>
      <guid isPermaLink="false">20958@/two/discussions</guid>
      <description><![CDATA[<p>Hi, i've a few problems, I want to create a sketch which generate randomly falling triangle. I've already achieve to make one falling triangle, but i don't really understand how array/table works in js, to generate more than one triangle. All my triangles are generate at the same position.</p>

<p>Oh and I'm in instance mode.</p>

<pre><code>var sketch = function (p) {
 var t1,t2,t3,t4,t5,t6,time;
   p.setup = function () {
     p.createCanvas(p.windowWidth, p.windowHeight);
     for(var i=0;i&lt;10;i++){
     generate();
   }
   };
    generate = function () {
     this.t1 = p.random(0,p.windowWidth);
     this.t3 = this.t1+p.random(-150,120);
     this.t5 = this.t1+p.random(-120,150);
     this.t2 = 300;
     this.t4 = p.random(-150,120);
     this.t6 = p.random(-120,150);
     this.time = p.random(0.5,2.5);
     p.print(this.t1);
   }
   p.draw = function () {
   p.background(150);
   for(var i=0;i&lt;10;i++){
     animate();
   }
   };
     animate = function (){
     p.triangle(this.t1,this.t2,this.t3,this.t2+this.t4,this.t5,this.t2+this.t6);
     this.t2+=this.time;
     p.print(this.t1);
     if(this.t2&gt;p.windowHeight){
     generate();
     }
   }
 }
 new p5(sketch, pong);
</code></pre>
]]></description>
   </item>
   <item>
      <title>How to call static p5.Vector methods in instance mode</title>
      <link>https://forum.processing.org/two/discussion/20509/how-to-call-static-p5-vector-methods-in-instance-mode</link>
      <pubDate>Fri, 27 Jan 2017 23:58:33 +0000</pubDate>
      <dc:creator>wigwam</dc:creator>
      <guid isPermaLink="false">20509@/two/discussions</guid>
      <description><![CDATA[<p>Hi so in instance mode with instance <code>p= new p5(..)</code>, I can make a vector <code>v1 = p.createVector()</code> and can then call <code>v1.dist(v2)</code> but I cannot call <code>p5.Vector.dist(v1,v2)</code> nor <code>p.p5.Vector</code>. This may be a general js question but I was wondering how to call p5.Vector functions like <code>angleBetween()</code> or <code>random2D()</code> etc.</p>
]]></description>
   </item>
   <item>
      <title>Modifying copy of an object without modifying original object</title>
      <link>https://forum.processing.org/two/discussion/20016/modifying-copy-of-an-object-without-modifying-original-object</link>
      <pubDate>Sun, 01 Jan 2017 08:05:58 +0000</pubDate>
      <dc:creator>Mysterion</dc:creator>
      <guid isPermaLink="false">20016@/two/discussions</guid>
      <description><![CDATA[<p>I have two sketches, and in sketch 1, I have an object that is storing all the relevant data in sketch 1. In sketch 2, I would like to make a copy of the object, and make minor modifications. However, making these minor modifications is also changing sketch 1's object. How can I get it to not do this?</p>

<p>Here's a minimal example:</p>

<p>Output I want: The circles in sketch 1 have the same height, while the circles in sketch 2 have different heights</p>

<p>Output I get: Circles in both sketch 1 and 2 have different heights.</p>

<pre><code>var sketch = function(p) {

  p.eyes = new Eyes();
    p.setup = function() {
        p.createCanvas(200,200);
        p.background(255);
    }
    p.draw = function() {
        p.ellipse(p.eyes.left[0], p.eyes.left[1], 10);
        p.ellipse(p.eyes.right[0], p.eyes.right[1],10);
    }
}

var sketch2 = function(p) {

  p.newEyes = main.eyes;
  p.newEyes.left = [10,10];
  p.newEyes.right = [50,50];

    p.setup = function() {
        p.createCanvas(200,200);
        p.background(255);
    }
    p.draw = function() {
        p.ellipse(p.newEyes.left[0], p.newEyes.left[1], 10);
        p.ellipse(p.newEyes.right[0], p.newEyes.right[1],10);
    }  
}

function Eyes() {
    this.left = [10,50];
    this.right = [50,50]; 
}

var main = new p5(sketch, 'canvasDiv'); 
var main2 = new p5(sketch2, 'canvasDiv');
</code></pre>
]]></description>
   </item>
   <item>
      <title>Couple of questions about instance mode</title>
      <link>https://forum.processing.org/two/discussion/19768/couple-of-questions-about-instance-mode</link>
      <pubDate>Thu, 15 Dec 2016 17:20:28 +0000</pubDate>
      <dc:creator>Tsskyx</dc:creator>
      <guid isPermaLink="false">19768@/two/discussions</guid>
      <description><![CDATA[<p>I don't know how to properly do some stuff. This is how instance mode usually looks like, right?</p>

<pre lang="javascript">
var sketch = function(p) {

  p.setup = function() {
  };

  p.draw = function() {
  };
};

new p5(sketch);
</pre>

<p>I see that functions just get rewritten with the constructor function syntax, that is instead of writing <code>function name()</code>, you write <code>object.name = function()</code>.</p>

<p>I get that. However, what if I want to write my own function? Do I need to do it with the same syntax? Do I need to include the <code>object.</code> part?</p>

<p>Each right curly bracket is ended with a semicolon - is that a necessity for instance mode?</p>

<p>In the official examples of instance mode, the last line is written like this: <code>var myp5 = new p5(s);</code>, while <strong><a rel="nofollow" href="https://github.com/processing/p5.js/wiki/Instantiation-Cases">here</a></strong> it is written like this: <code>new p5(sketch);</code>. So, which one is correct?</p>
]]></description>
   </item>
   <item>
      <title>How to use p5 without a sketch, with react native</title>
      <link>https://forum.processing.org/two/discussion/19220/how-to-use-p5-without-a-sketch-with-react-native</link>
      <pubDate>Tue, 22 Nov 2016 16:52:26 +0000</pubDate>
      <dc:creator>koranlary</dc:creator>
      <guid isPermaLink="false">19220@/two/discussions</guid>
      <description><![CDATA[<p>I would like to know if it is possible to use the p5 libraries without a sketch.  I would like to be able to use p5js with the react native framework to get a p5 project onto a mobile device.</p>

<p>Can this be done?</p>
]]></description>
   </item>
   <item>
      <title>Putting entire sketch inside a function</title>
      <link>https://forum.processing.org/two/discussion/18198/putting-entire-sketch-inside-a-function</link>
      <pubDate>Sun, 18 Sep 2016 09:49:32 +0000</pubDate>
      <dc:creator>sai</dc:creator>
      <guid isPermaLink="false">18198@/two/discussions</guid>
      <description><![CDATA[<p>I am working on an online artwork generator using the flickr API. For the prototype I want a user to enter a string into an input box and then flickr returns images based on that string that are then overlayed etc to make a new image.</p>

<p>This is my code so far:</p>

<p>`
              
              </p><p>
                Type a word to generate a new image
              </p>

<pre><code>          &lt;input type="text" id="subject" value="rainbow"&gt;

          &lt;button onclick="createNewImage()"&gt;Create New Image&lt;/button&gt;

          &lt;script language="javascript" type="text/javascript"&gt;


          //The problem occurs with the calling of this function, if I remove it it works but I have to call it after user input....
          function createNewImage(){


            var keyword = document.getElementById("subject").value;


            $.getJSON("<a href="http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=" target="_blank" rel="nofollow">http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=</a>?",
            {
              tags: keyword,
              tagmode: "any",
              format: "json"
            },

            function(data) {
              var image_1_index = Math.floor(Math.random() * data.items.length);
              var image_2_index = Math.floor(Math.random() * data.items.length);

              image1_src = data.items[image_1_index]['media']['m'].replace("_m", "_b");
              image2_src = data.items[image_2_index]['media']['m'].replace("_m", "_b");
              //$('body').css('background-image', "url('" + image_src + "')");

            });

            var blend_index;

            var xa, yb, xc, yd, xe, yf, xg, yh, r, g, b;

            function preload() {
              img1 = loadImage(image1_src);
              img2 = loadImage(image2_src);
              imgs = [loadImage("assets/1.jpg"), loadImage("assets/2.jpg"), loadImage("assets/3.jpg"), loadImage("assets/4.jpg"), loadImage("assets/5.jpg"), loadImage("assets/0.jpg")];
            }

            function setup() {
              frameRate(60);
              createCanvas(windowWidth, windowHeight);
              image_index = int(random(5));
            }



            function draw() {

              blend_index = int(random(4));

              switch (blend_index) {
                case 0: blendMode(ADD);
                break;
                case 1: blendMode(DARKEST);
                break;
                case 2: blendMode(LIGHTEST);
                break;
                case 3: blendMode(DIFFERENCE);
                break;
                case 4: blendMode(EXCLUSION);
                break;

                default: blendMode(ADD);

              }

              image(img1, 0, 0, windowWidth, windowHeight);
              image(img2, 0, 0,  windowWidth, windowHeight);
              noLoop();

            }

          } //end create new image function

          function windowResized() {
            resizeCanvas(windowWidth, windowHeight);
          }



          &lt;/script&gt;`
</code></pre>

<p>This all works fine until I try to call the 'createNewImage' function, what could cause this? Thank you in advance.</p>
]]></description>
   </item>
   <item>
      <title>Instance mode, adding and removing sketches, creating multiple sketches with a loop</title>
      <link>https://forum.processing.org/two/discussion/18082/instance-mode-adding-and-removing-sketches-creating-multiple-sketches-with-a-loop</link>
      <pubDate>Mon, 05 Sep 2016 18:59:45 +0000</pubDate>
      <dc:creator>Burans</dc:creator>
      <guid isPermaLink="false">18082@/two/discussions</guid>
      <description><![CDATA[<p>Hi! I really need to clarify some things regarding instance mode. I hope someone here can help.</p>

<ol>
<li><p>I have created 3 sketches and I would like to use them as decorative header backgrounds for different pages of a website. The website uses js to pull in content so there is no refresh when user switches between pages - i have to dynamically add and remove corresponding sketches to the header. My idea was to run the sketches in instance mode, assign the necessary sketch to a var - <code>headerBgSketch = new p5(sketch);</code> and run <code>headerBgSketch.remove();</code> before assigning the next one. For some reason the <code>remove</code> part is not working. I get "Cannot read property 'remove' of undefined". Is this the right way to do something like that?</p></li>
<li><p>The landing page of the same site has a slider in header instead of the decorative backgrounds. I am using p5 to color correct the images inside the slider. I am using a loop to create a sketch for each image like this:</p></li>
</ol>

<p>$(document).ready(function(){
    var imgCount = $('.header-img-cont').length;
        for(c = 1; c &lt; imgCount+1; c++){</p>

<pre><code>        new p5(function (h) {

            var bgimg;
            var src = $('#header-img-cont-'+c).data('src-v');

            h.preload = function() {
                bgimg = h.loadImage(src);
            }

            h.setup = function() {
                h.createCanvas(headerW, headerH);
                drawImage();
            }

            function drawImage() {
                //Draws the image and does some color correcting. I don't need the draw loop for this. I just need to draw it once.

            }

        }, "header-img-cont-"+c);

    }
</code></pre>

<p>});</p>

<p>I seems to be working for now. I am just interested if its the right way to do this.</p>

<p>Any comments will be greatly appreciated!
Thanks!</p>
]]></description>
   </item>
   <item>
      <title>Using createVector() on a class error. what should the right order be?</title>
      <link>https://forum.processing.org/two/discussion/17841/using-createvector-on-a-class-error-what-should-the-right-order-be</link>
      <pubDate>Sun, 14 Aug 2016 18:22:51 +0000</pubDate>
      <dc:creator>giorgiomartini</dc:creator>
      <guid isPermaLink="false">17841@/two/discussions</guid>
      <description><![CDATA[<p>I got this sketch : <a rel="nofollow" href="http://codepen.io/giorgiomartini/pen/oLJyBj?editors=0011">http://codepen.io/giorgiomartini/pen/oLJyBj?editors=0011</a> and I want to have a point class, that draws the point using a vector.</p>

<p>But i get an error in the codepen console and it doesnt draw the point on the screen: <code>Did you just try to use p5.js's createVector() function? If so, you may want to move it into your sketch's setup() function</code></p>

<p>But.. the createVector function is and i think it should stay on my point class...</p>

<p>Any idea where's the problem?</p>

<p>Thanks :)</p>
]]></description>
   </item>
   <item>
      <title>Problems with namespacing.....</title>
      <link>https://forum.processing.org/two/discussion/17421/problems-with-namespacing</link>
      <pubDate>Tue, 05 Jul 2016 20:20:17 +0000</pubDate>
      <dc:creator>kennyrosenyc</dc:creator>
      <guid isPermaLink="false">17421@/two/discussions</guid>
      <description><![CDATA[<p>So I created this massive page with 5 sketches and 5 canvases it works fine on my computer but when I uploaded it I got this error "SCRIPT5009: 'p5' is undefined". I don't get it.</p>

<p>Here is the code (no cracks about the complete mess that it is thank you) :</p>

<p>var myp5_1 = new p5(function(a1) {</p>

<pre><code>var time = 0;
var bx = 0;
var tx = 65;
var ty = 295;
var eRa = 255;

a1.preload = function() {
    img1 = a1.loadImage('images/Stuff/banners/Sea/clouds1.png');
    img2 = a1.loadImage('images/Stuff/banners/Sea/clouds2.png');
    img3 = a1.loadImage('images/Stuff/banners/Sea/KRD.png');
    img4 = a1.loadImage('images/Stuff/banners/Sea/skills.png');
}

a1.setup = function() {

    a1.canvas = a1.createCanvas(900, 300);
    a1.canvas.parent("#banner1");
    a1.background(0);
    a1.smooth();

}

a1.mousePressed = function() {

    if (a1.mouseY &lt; a1.height &amp;&amp; a1.mouseY &gt; 5 &amp;&amp; a1.mouseX &lt; a1.width &amp;&amp; a1.mouseX &gt; 5) {

        a1.draw = function() {

            a1.background(75, 100, 127);

            a1.image(img3, tx, 50);

            a1.noStroke();
            a1.fill(75, 100, 127);
            a1.rect(0, 0, a1.width / 2, a1.height)

            var t1 = 0;
            var ty2 = ty;

            while (t1 &lt;= 10) {
                a1.image(img4, 10, ty2);
                t1 = t1 + 1;
                ty2 = ty + 280 * t1;

            }

            var i1 = 0;
            var ix1 = bx;

            while (i1 &lt;= 5) {
                a1.image(img1, ix1, 0);
                i1 = i1 + 1;
                ix1 = bx + 900 * i1;
            }

            var i2 = 0;
            var ix2 = bx;

            while (i2 &lt;= 25) {
                a1.image(img2, ix2, 0);
                i2 = i2 + 1;
                ix2 = bx + 900 * i2;
            }

            a1.noFill();
            var x = 0;
            a1.stroke(0);

            while (x &lt; a1.width) {
                a1.line(x, 200 + 50 * a1.noise(x / 300, time), x, a1.height);
                x = x + 1;
            }

            time = time + 0.02;
            bx = bx - 1;
            tx = tx + 4;

            if (tx &gt;= 450) {
                tx = 450;
                ty = ty - 4;
            }

            if (ty &lt;= -1000) {
                a1.fill(0, eRa);
                a1.rect(0, 0, 900, 300);
                a1.noLoop();
            }

        }

    }

}
</code></pre>

<p>});</p>

<p>var myp5_2 = new p5(function(a2) {</p>

<pre><code>var m = 0;
var cs = 0;
ty = 325;
ty2 = -25;

a2.setup = function() {

    a2.canvas = a2.createCanvas(900, 300);
    a2.canvas.parent("#banner2");
    a2.background(255);
    a2.smooth();

}

a2.mousePressed = function() {
    if (a2.mouseY &lt; a2.height &amp;&amp; a2.mouseY &gt; 5 &amp;&amp; a2.mouseX &lt; a2.width &amp;&amp; a2.mouseX &gt; 5) {

        a2.draw = function() {

            if (m &lt; 200) {
                a2.stroke(cs);
                a2.strokeWeight(1);
                a2.noFill();
                a2.ellipse(150 + m * 2, 150, 300 - m * 5, 300 - m * 3.3);
                m = m + 1;
            }

            cs = cs + 1;

            if (m == 200) {
                a2.stroke(100, 0, 0);
                a2.strokeWeight(4);
                a2.fill(255);
                a2.textSize(40);
                a2.textFont('Verdana');
                a2.text("Kenny", 360, ty);
                a2.text("Rose", 495, ty2);
                a2.text("Design", 600, ty);

                if (ty &gt; 150) {
                    ty = ty - 3;
                } else if (ty &lt;= 150) {
                    ty = 150;
                }

                if (ty2 &lt; 150) {
                    ty2 = ty2 + 3;
                } else if (ty &gt;= 150) {
                    ty2 = 150;
                }

            }



        }
    }

}
</code></pre>

<p>});</p>

<p>var myp5_3 = new p5(function(a3) {</p>

<pre><code>var r = 0;
var lx = 0;
var ta = 0;

a3.setup = function() {

    a3.canvas = a3.createCanvas(900, 300);
    a3.canvas.parent("#banner3");
    a3.background(127);
    a3.noStroke();
    a3.fill(10, 100);
    a3.rect(675, 50, 225, 300);
    a3.rect(520, 10, 150, 300);
    a3.rect(165, 100, 350, 300);
    a3.rect(0, 25, 160, 300);
    a3.smooth();

}

a3.mousePressed = function() {
    if (a3.mouseY &lt; a3.height &amp;&amp; a3.mouseY &gt; 5 &amp;&amp; a3.mouseX &lt; a3.width &amp;&amp; a3.mouseX &gt; 5) {

        a3.draw = function() {

            var es = a3.random(10, 30);
            a3.fill(255, 0, 0, 25);
            a3.stroke(255, 200, 0, 25);
            a3.strokeWeight(es / 5);
            a3.rotate(r);
            a3.ellipse(40 + r, 0, es, es);
            r = r + 0.2;

            a3.resetMatrix();

            a3.noStroke();
            a3.fill(0, ta);
            a3.textSize(40);
            a3.text("Kenny Rose Design", 450, 100)

            var ly = a3.random(200, 285);
            a3.stroke(12, 50, 12, 75);
            a3.strokeWeight(es / 5);
            a3.line(lx, 300, lx, ly);
            lx = a3.random(0, 900);

            if (r &gt; 675) {
                ta = ta + 1;
            }

            if (r &gt; 910) {
                r = 910;
                a3.fill(127);
                a3.rect(0, 0, 900, 300);
                a3.noLoop();
            }

        }

    }

}
</code></pre>

<p>});</p>

<p>var myp5_4 = new p5(function(a4) {</p>

<pre><code>var ex1 = -15;
var ey1 = 200;
var er1 = 0;
var ex2 = 915;
var ey2 = 25;
var ex3 = 900;
var ey3 = 320;
var ex4 = 20;
var ey4 = -10;
var rx1 = 905;
var ry1 = 90;
var ts = 0;


a4.setup = function() {

    a4.canvas = a4.createCanvas(900, 300);
    a4.canvas.parent("#banner4");
    a4.background(50, 25, 0);
    a4.noStroke();
    a4.fill(75, 50, 0);
    a4.rect(20, 20, 860, 260, 10);
    a4.smooth();

}

a4.mousePressed = function() {
    if (a4.mouseY &lt; a4.height &amp;&amp; a4.mouseY &gt; 5 &amp;&amp; a4.mouseX &lt; a4.width &amp;&amp; a4.mouseX &gt; 5) {

        a4.draw = function() {

            a4.stroke(255, 128, 128, ts);
            a4.strokeWeight(1);
            a4.fill(255, 128, 128);
            a4.textSize(53);
            a4.text("Kenny Rose Design", 130, 150)

            a4.noStroke();
            a4.fill(128, 255, 128);
            a4.ellipse(ex4, ey4, 20, 20);
            a4.ellipse(ex4 + 30, ey4, 20, 20);
            a4.ellipse(ex4 + 60, ey4, 20, 20);
            a4.ellipse(ex4 + 90, ey4, 20, 20);

            a4.noStroke();
            a4.fill(255, 255, 128);
            a4.rect(rx1, ry1, 2, 105);

            a4.noStroke();
            a4.noFill();
            a4.ellipse(ex3, ey3, 200, 200);
            a4.noStroke();
            a4.fill(255, 128, 128);
            a4.ellipse(ex3 - 100, ey3, 20, 20);
            a4.fill(255, 255, 128);
            a4.ellipse(ex3 - 120, ey3, 20, 20);
            a4.fill(128, 255, 128);
            a4.ellipse(ex3 - 140, ey3, 20, 20);

            a4.noStroke();
            a4.noFill();
            a4.ellipse(ex2, ey2, 200, 200);
            a4.noStroke();
            a4.fill(255, 128, 128);
            a4.ellipse(ex2, ey2 + 10, 20, 20);
            a4.fill(255, 255, 128);
            a4.ellipse(ex2, ey2 + 30, 20, 20);
            a4.fill(128, 255, 128);
            a4.ellipse(ex2, ey2 + 50, 20, 20);

            a4.translate(ex1, ey1);
            a4.rotate(a4.radians(er1));
            a4.noStroke();
            a4.noFill();
            a4.ellipse(0, 0, 200, 200);
            a4.noStroke();
            a4.fill(255, 128, 128);
            a4.ellipse(0, 10, 20, 20);
            a4.fill(255, 255, 128);
            a4.ellipse(0, 30, 20, 20);
            a4.fill(128, 255, 128);
            a4.ellipse(0, 50, 20, 20);

            a4.resetMatrix();

            if (ex1 &gt; 600) {
                ex1 = 600;
                er1 = er1 - 5;
            }

            if (er1 &lt; -90) {
                er1 = -90;
                ey1 = ey1 - 5;
            }

            ex1 = ex1 + 5;

            if (ex2 &lt; -10) {
                ex2 = -10;
                ey3 = ey3 - 5;
            }

            ex2 = ex2 - 5;

            if (ey3 &lt; -20) {
                ey3 = -20;
                ey4 = ey4 + 30;
            }

            if (ey4 &gt; 310) {
                ey4 = 310;
                rx1 = rx1 - 4;
                ts = ts + 1;
            }

            if (rx1 &lt; 635) {
                rx1 = 635;
            }

        }
    }
}
</code></pre>

<p>});</p>
]]></description>
   </item>
   <item>
      <title>multiple canvases on one page</title>
      <link>https://forum.processing.org/two/discussion/17306/multiple-canvases-on-one-page</link>
      <pubDate>Sun, 26 Jun 2016 08:23:53 +0000</pubDate>
      <dc:creator>allonestring</dc:creator>
      <guid isPermaLink="false">17306@/two/discussions</guid>
      <description><![CDATA[<p>I can't get more than one sketch to run on a webpage. Each works fine when it's on its own. I have in the sketches</p>

<pre><code>var c1 = createCanvas(200, 200);
c1.parent('firstcanvas');
</code></pre>

<p>and</p>

<pre><code>var c2 = createCanvas(300, 300);
c2.parent('secondcanvas');
</code></pre>

<p>and, in the html</p>

<pre><code>&lt;div id = "firstcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;div id = "secondcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/secondscript.js" type="text/javascript"&gt;&lt;/script&gt;
</code></pre>

<p>Inspect shows</p>

<pre><code>&lt;div id="firstcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;canvas id="defaultCanvas0" width="200" height="200" style="width: 200px; height: 200px;"&gt;&lt;/canvas&gt;
&lt;div id = "secondcanvas"&gt;&lt;/div&gt;
&lt;script src="pathto/secondscript.js" type="text/javascript"&gt;&lt;/script&gt;
</code></pre>

<p>and the webpage just shows the first sketch. I've tried various combos of div and canvas, with no luck.</p>

<pre><code>&lt;div id="firstcanvas"&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;canvas id="firstcanvas" width="200" height="200" style="width: 200px; height: 200px;"&gt;&lt;/canvas&gt;
&lt;/div&gt;
</code></pre>

<p>and</p>

<pre><code>&lt;canvas id="firstcanvas"&gt;
&lt;script src="pathto/firstscript.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;/canvas&gt;
</code></pre>

<p>with no luck. If I put ordinary javascript animations on the page, as long as I target the right canvas id, they work fine together.
Any ideas?</p>
]]></description>
   </item>
   <item>
      <title>Using instance mode to create multiple sketches on the same page</title>
      <link>https://forum.processing.org/two/discussion/17332/using-instance-mode-to-create-multiple-sketches-on-the-same-page</link>
      <pubDate>Mon, 27 Jun 2016 21:59:26 +0000</pubDate>
      <dc:creator>blindfish</dc:creator>
      <guid isPermaLink="false">17332@/two/discussions</guid>
      <description><![CDATA[<p>Since this question crops up regularly I figured I'd write a separate post with a clear title so hopefully it will be easy to find...</p>

<p>To understand <a rel="nofollow" href="https://github.com/processing/p5.js/wiki/Instantiation-Cases">instance mode</a> you have to understand what's happening inside p5 when you invoke it.  The official <a rel="nofollow" href="https://p5js.org/examples/examples/Instance_Mode_Instantiation.php">example</a> passes a callback function named 'sketch' to the p5 constructor:</p>

<p><code>var myp5 = new p5(sketch);</code></p>

<p>Here's a simplified representation of what p5 does with that callback:</p>

<pre><code>// a pseudo class:
var iamP5 = function(callback) {

    this.name = "P5 impostor";
    this.someProperty = "foo";

    // *** pass a reference to the newly created object into the callback ***
    callback(this);
};

// pseudo-class method:
iamP5.prototype.sayName = function() {
    console.info(this.name);
};

// instantiate the pseudo-class passing it our callback function
new iamP5 (sketch);


// the callback being passed to foo.  Could equally be 
// a directly passed anonymous function
function sketch(input) {
  // Here 'input' is the 'this' being passed to callback() in iamP5's 'constructor'.
  // It therefore acts as a reference to the instance of foo created above.
  // Any property or method exposed by foo can be accessed via this variable.
  // If you need to reference it a lot you might choose a shorter variable 
  // name such as 'p'; which is what is generally recommended with p5...
  console.info(input.someProperty);
  input.sayName();

  // if they aren't protected you can override properties 
  // and methods of the instantiated object (see notes below)
  input.sayName = function() {
      console.log("I am a robot");
  }
  input.sayName();
}
</code></pre>

<p>It's obviously significantly more complex than that in reality but I think that's enough to explain the code structure...  The ability to override or add properties and methods to the instance of p5 is important for two reasons:</p>

<ol>
<li>it allows us to pass p5 our own customised setup(); draw() and other functions</li>
<li>it is otherwise very risky to add your own properties or method to the variable referencing p5 (commonly 'p').  The whole point of using instance mode is to protect the global scope (so that, for example, your global variables won't clash with p5 variables).  If you start attaching properties/methods to the p5 reference you again risk name clashes.</li>
</ol>

<p>It's worth mentioning that the ability to pass a function as a parameter to another function - as demonstrated above - is one of the features that makes JS such a powerful and flexible language.  It's also a feature that, if not applied carefully, can lead to brittle, hard to maintain code ;)</p>

<p><em>This post is intended to be helpful to people unfamiliar with instance mode.  Feedback is most welcome.</em></p>
]]></description>
   </item>
   <item>
      <title>placing slider relative to canvas in instance mode</title>
      <link>https://forum.processing.org/two/discussion/17074/placing-slider-relative-to-canvas-in-instance-mode</link>
      <pubDate>Thu, 09 Jun 2016 21:00:29 +0000</pubDate>
      <dc:creator>aatish</dc:creator>
      <guid isPermaLink="false">17074@/two/discussions</guid>
      <description><![CDATA[<p>I'm trying to place a slider in a p5 sketch running in instance mode. However, the slider position is calculated relative to the html page, and not relative to the origin of the canvas. So for example if the canvas is in a different location, the slider won't move with it.</p>

<p>How can I place the slider relative to the canvas? (i.e. in a manner similar to other p5 objects, like images, etc.)</p>

<p>Here's my test code:</p>

<pre><code>var testslider = function(p){

    var slider;

    p.setup = function() {
      p.createCanvas(400, 400);
      p.background(200);
      slider = p.createSlider(0, 255, 100);
      slider.position(0, 20);
    }

    p.draw = function() {
    }

}
</code></pre>

<p>Thanks!</p>
]]></description>
   </item>
   </channel>
</rss>